import { createStore, applyMiddleware } from "redux";
import thunk from "redux-thunk";
// immutable合并多个reducer ，并且返回immutable对象  npm i -S redux-immutable
/**
 * 步骤一：引入合并reducer模块，此模块可以防止immutable对象的带来的麻烦
 */
import { combineReducers } from "redux-immutable";
// 开发调试工具,需要在chrome中安装插件才能使用
import { composeWithDevTools } from "redux-devtools-extension";

// 自动化导入 reducer
const moduleFn = require.context("../reducer", false, /\.js$/i);
let modules = moduleFn.keys().reduce((returnValue, currentValue) => {
	// key用文件名称
	let key = currentValue.match(/\.\/(\w+)\.js$/)[1];
	returnValue[key] = moduleFn(currentValue).default;
	return returnValue;
}, {});

// 合并多个reducer
const reducer = combineReducers(modules);
// 创建redux仓库
const store = createStore(reducer, composeWithDevTools(applyMiddleware(thunk)));

export default store;
